package com.lyh.hkcloud.module.m1300028.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.module.m1300026.IndexActivity;
import com.lyh.hkcloud.util.base.JsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description: 数据适配器
 * @company—date: 利元数据 2022-02-14 15:12
 **/

public class ListViewAdapter extends BaseAdapter   {

    private Context context;
    private List<HashMap<String,Object>> dataMap;
    private String inventoryId;
    private String inventoryStatus;
    //private IndexActivity mContext;

    /**
     * 构造器
     *
     * @param context  上下文对象
     * @param dataMap 数据
     */
    public ListViewAdapter(Context context, List<HashMap<String,Object>> dataMap,String inventoryId,String inventoryStatus) {
        this.context = context;
        this.dataMap = dataMap;
        this.inventoryId=inventoryId;
        this.inventoryStatus = inventoryStatus;
    }

    @Override
    public int getCount() {
        return dataMap.size();
    }

    @Override
    public Object getItem(int position) {
        return dataMap.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_listview_item_1300028,parent,false);
            holder = new ViewHolder();
            holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
            holder.tv_fatype = (TextView) convertView.findViewById(R.id.tv_fatype);
            holder.tv_line_state = (TextView) convertView.findViewById(R.id.tv_line_state);
            holder.tv_man_umber = (TextView) convertView.findViewById(R.id.tv_man_umber);
            holder.tv_goto_memo = (TextView) convertView.findViewById(R.id.tv_goto_memo);
            //holder.tv_goto_memo.setOnClickListener(ListViewAdapter.this);
            holder.tv_sys_qty = (TextView) convertView.findViewById(R.id.tv_sys_qty);
            holder.et_inv_qty = (EditText) convertView.findViewById(R.id.et_inv_qty);
            //holder.ll_details = convertView.findViewById(R.id.ll_details);

            convertView.setTag(holder);   //将Holder存储到convertView中
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_id.setText(dataMap.get(position).get(BusinessConstants.FA_ID).toString());
        holder.tv_fatype.setText(dataMap.get(position).get(BusinessConstants.FA_TYPE).toString());
        holder.tv_line_state.setText(dataMap.get(position).get(BusinessConstants.LINE_STATE).toString());
        holder.tv_man_umber.setText(dataMap.get(position).get(BusinessConstants.MAN_UMBER).toString());
        holder.tv_sys_qty.setText(dataMap.get(position).get(BusinessConstants.SYS_QTY).toString());
        holder.et_inv_qty.setText(dataMap.get(position).get(BusinessConstants.INV_QTY).toString());
        if (BusinessConstants.POSTED.equals(dataMap.get(position).get(BusinessConstants.LINESTATE))) {
            holder.tv_goto_memo.setTextColor(Color.GRAY);
            //holder.tv_goto_memo.setOnClickListener(null);
        }


        holder.tv_goto_memo.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.v("ll_details","执行盘点备注"+ position);

                //盘点差异备注页
                Map dataNoteMap = new HashMap<String,Object>();
                Intent intent = new Intent(context, com.lyh.hkcloud.module.m1300029.IndexActivity.class);
                dataNoteMap.put(BusinessConstants.INVENTORY_ID,inventoryId);
                dataNoteMap.put(BusinessConstants.INVENTORY_STATUS,inventoryStatus);
                dataNoteMap.put(BusinessConstants.FA_ID,dataMap.get(position).get(BusinessConstants.FA_ID).toString());
                intent.putExtra(BusinessConstants.DATA_ID, JsonUtil.toJSONObject(dataNoteMap).toString());
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    /*@Override
    public void onClick(View v) {
        Intent intent =null;
        switch (v.getId()) {
            case R.id.tv_goto_memo:
                //盘点差异备注页
                Map dataMap = new HashMap<String,Object>();
                intent = new Intent(context, com.lyh.hkcloud.module.m1300029.IndexActivity.class);
                dataMap.put(BusinessConstants.INVENTORY_ID,inventoryId);
                dataMap.put(BusinessConstants.INVENTORY_STATUS,inventoryStatus);
                //TextView tv_id=v.findViewById(R.id.tv_id);
                //dataMap.put("dataid",tv_id.getText());
                intent.putExtra(BusinessConstants.DATA, JsonUtil.toJSONObject(dataMap).toString());
                context.startActivity(intent);
                break;
        }
    }*/


    /**
     * ViewHolder类
     */
    private final class ViewHolder {
        TextView tv_id;
        //分类名称
        TextView tv_fatype;
        //盘点状态
        TextView tv_line_state;
        //物料编号
        TextView tv_man_umber;

        TextView tv_goto_memo;

        TextView tv_sys_qty;

        EditText et_inv_qty;

        //LinearLayout ll_details;

    }
}

